# -*- coding: utf-8 -*-

import SCons.Tool
import SCons.Util
import os.path

CSuffixes = ['.c', '.m']
CXXSuffixes = ['.cpp', '.cc', '.cxx', '.c++', '.C++', '.mm', '.C']

def iscplusplus(source):
    if not source:
        return False
    for s in source:
        if s.sources:
            ext = os.path.splitext(str(s.sources[0]))[1]
            if ext in CXXSuffixes:
                return True
    return False

def smart_link(source, target, env, for_signature):
    if iscplusplus(source):
        return '$CXX'
    return '$CC'

def generate(env):
    # setting up SCons magic
    static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
    for suffix in CSuffixes:
        static_obj.add_action(suffix, SCons.Defaults.CAction)
        shared_obj.add_action(suffix, SCons.Defaults.ShCAction)
        static_obj.add_emitter(suffix, SCons.Defaults.StaticObjectEmitter)
        shared_obj.add_emitter(suffix, SCons.Defaults.SharedObjectEmitter)
    for suffix in CXXSuffixes:
        static_obj.add_action(suffix, SCons.Defaults.CXXAction)
        shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction)
        static_obj.add_emitter(suffix, SCons.Defaults.StaticObjectEmitter)
        shared_obj.add_emitter(suffix, SCons.Defaults.SharedObjectEmitter)

    SCons.Tool.createStaticLibBuilder(env)
    SCons.Tool.createSharedLibBuilder(env)
    SCons.Tool.createProgBuilder(env)

    env['AR'] = '${_TOOLCHAINPREFIX}ar'
    env['ARFLAGS'] = SCons.Util.CLVar('')
    env['ARCOM'] = '$AR $ARFLAGS $TARGET $SOURCES'
    env['LIBPREFIX'] = 'lib'
    env['LIBSUFFIX'] = '.a'

    env['RANLIB'] = 'ranlib'
    env['RANLIBFLAGS'] = SCons.Util.CLVar('')
    env['RANLIBCOM'] = '$RANLIB $RANLIBFLAGS $TARGET'

    if not env.has_key('_CCCOMCOM'):
        env['_CCCOMCOM'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS'

    env['CC'] = '${_TOOLCHAINPREFIX}gcc'
    env['CFLAGS'] = SCons.Util.CLVar('')
    env['CCFLAGS'] = SCons.Util.CLVar('')
    env['CCCOM'] = '$CC -o $TARGET -c $CFLAGS $CCFLAGS $_CCCOMCOM $SOURCES'
    env['SHCC'] = '$CC'
    env['SHCFLAGS'] = SCons.Util.CLVar('$CFLAGS')
    env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
    env['SHCCCOM'] = '$SHCC -o $TARGET -c $SHCFLAGS $SHCCFLAGS $_CCCOMCOM $SOURCES'

    env['CXX'] = '${_TOOLCHAINPREFIX}g++'
    env['CXXFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
    env['CXXCOM'] = '$CXX -o $TARGET -c $CXXFLAGS $_CCCOMCOM $SOURCES'
    env['SHCC'] = '$CXX'
    env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
    env['SHCXXCOM'] = '$SHCXX -o $TARGET -c $SHCXXFLAGS $_CCCOMCOM $SOURCES'

    env['CPPDEFPREFIX'] = '-D'
    env['CPPDEFSUFFIX'] = ''
    env['INCPREFIX'] = '-I'
    env['INCSUFFIX'] = ''
    env['OBJSUFFIX'] = '.o'
    env['SHOBJSUFFIX'] = '.os'
    env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 0

    env['CFILESUFFIX'] = '.c'
    env['CXXFILESUFFIX'] = '.cpp'

    env['SMARTLINK'] = smart_link
    env['LINK'] = '$SMARTLINK'
    env['LINKFLAGS'] = SCons.Util.CLVar('')
    env['LINKCOM'] = '$LINK -o $TARGET $LINKFLAGS $SOURCES $_LIBDIRFLAGS $_LIBFLAGS'
    env['LIBDIRPREFIX'] = '-L'
    env['LIBDIRSUFFIX'] = ''
    env['_LIBFLAGS'] = '${_stripixes(LIBLINKPREFIX, LIBS, LIBLINKSUFFIX, LIBPREFIX, LIBSUFFIX, __env__)}'
    env['LIBLINKPREFIX'] = '-l'
    env['LIBLINKSUFFIX'] = ''

    env['SHLINK'] = '$LINK'
    env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
    env['SHLINKCOM'] = '$SHLINK -o $TARGET $SHLINKFLAGS $SOURCES $_LIBDIRFLAGS $_LIBFLAGS'


def exists(env):
    return 1
